"""扫描问题模型"""
from sqlalchemy import Column, Integer, String, Text, DateTime, ForeignKey, func
from sqlalchemy.orm import relationship
from app.database import Base


class Issue(Base):
    """扫描问题表"""
    __tablename__ = "scan_issues"

    id = Column(Integer, primary_key=True, index=True)
    record_id = Column(Integer, ForeignKey("scan_records.id", ondelete="CASCADE"), nullable=False, comment="扫描记录ID")
    issue_number = Column(String(50), nullable=False, comment="问题编号")
    rule_name = Column(String(200), nullable=False, comment="规则名称")
    level = Column(String(20), nullable=False, comment="级别: critical/warning/info")
    file_path = Column(String(500), nullable=False, comment="文件路径")
    line_number = Column(Integer, comment="行号")
    description = Column(Text, nullable=False, comment="问题描述")
    code_snippet = Column(Text, comment="主要代码片段（向后兼容，用于单个代码片段场景）")
    # 注意：多个代码片段应通过扩展字段存储（scan_issue_extensions表）
    # 例如：cangjie_code_snippet, arkts_code_snippet, old_value_code, new_value_code等
    confirm_status = Column(String(20), default="unconfirmed", comment="确认状态")
    confirm_type = Column(String(50), comment="确认类型: problem/falsePositive/fixed/known")
    confirmed_by = Column(String(100), comment="确认人")
    confirmed_at = Column(DateTime, comment="确认时间")
    confirm_comment = Column(Text, comment="确认备注")
    inherited_from_id = Column(Integer, ForeignKey("scan_issues.id"), comment="继承来源问题ID")
    created_at = Column(DateTime, server_default=func.now(), comment="创建时间")
    updated_at = Column(DateTime, server_default=func.now(), onupdate=func.now(), comment="更新时间")

    # 关系
    scan_record = relationship("ScanRecord", back_populates="issues")
    inherited_from = relationship("Issue", remote_side=[id])
    confirm_history = relationship("ConfirmHistory", back_populates="issue", cascade="all, delete-orphan")

